<?php
// 分页处理

class page {
	// 起始行数
	public $firstRow;
	// 列表每页显示行数
	public $listRows;
	// 页数跳转时要带的参数
	public $parameter;
	// 分页总页面数
	public $totalPages;
	// 总行数
	protected $totalRows;
	// 当前页数
	protected $nowPage;
	// 分页栏每页显示的页数
	protected $rollPage;
	// 分页配置信息
	protected $config  = array();
	// 分页条主题
	protected $theme  = array(
		'cn'=> array( 'prev'=>'上一页', 'next'=>'下一页', 'first'=>'首页', 'last'=>'末页', 'theme'=>' %totalRow% 条记录 %nowPage%/%totalPage% 页 %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%'),
		'en'=> array( 'prev'=>'Prev', 'next'=>'Next', 'first'=>'First', 'last'=>'Last', 'theme'=>' %totalRow% records %nowPage%/%totalPage% pages %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%'),
	);

	/**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @param int $totalRows  总的记录数
	 * @param int $totalRows  总的记录数
     * @param int $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
	public function __construct($totalRows,$nowpage='',$listRows='',$parameter=array()) {
		$this->setTheme('cn');
		$this->init($totalRows,$nowpage,$listRows,$parameter);
	}

	//初始化
	public function init($totalRows,$nowpage='',$listRows='',$parameter=array()){
		$this->parameter = $this->setParameter($parameter);
		$this->totalRows = (int)$totalRows; //总条数
		$nowpage = $nowpage ? $nowpage : ( (int)$_GET[C('var_page')]>0 ? (int)$_GET[C('var_page')] : 1 );
		$this->nowPage  = $nowpage; //当前页

		$this->rollPage = C('sys_page_rollpage'); //分页条显示页数
		$this->listRows = !empty($listRows)?$listRows:C('sys_page_listrows'); //每页条数
		$this->totalPages = (int)ceil($this->totalRows/$this->listRows);     //总页数
		//$this->totalPages = $this->totalPages ? $this->totalPages : 1;
		
		if($this->totalPages && $this->nowPage>$this->totalPages) {
			$this->nowPage = $this->totalPages;
		}
		$this->firstRow = $this->listRows*($this->nowPage-1);
	}

	//设置分类参数
	public function setConfig($name,$value) {
		$this->config[$name] = $value;
	}

	//设置分类条主题样式
	public function setTheme($name){
		if(!isset($this->theme[$name])) return ;
		foreach($this->theme[$name] as $k=>$v){
			$this->config[$k] = $v;
		}
	}

	//获取分页数组
	public function getInfo(){
		$arr = array();
		$arr['firstRow'] = $this->firstRow;
	}

	/**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
	public function show($type='string') {
		$firstNum = 1;
		$preNum = $this->nowPage - 1;
		$nextNum = $this->nowPage + 1;
		$lastNum = $this->totalPages;
		$linkPage = $this->pageNum();

		if ($this->nowPage == 1) {
			$firstPage = '<span>'.$this->config['first'].'</span>';
			$prePage = '<span>'.$this->config['prev'].'</span>';
		}else{
			$firstPage = '<a href="'.$this->getUrl($firstNum).'">'.$this->config['first'].'</a>';
			$prePage = '<a href="'.$this->getUrl($preNum).'">'.$this->config['prev'].'</a>';
		}

		if ($this->totalPages == $this->nowPage || $this->totalPages == 0) {
			$nextPage = '<span>'.$this->config['next'].'</span>';
			$lastPage = '<span>'.$this->config['last'].'</span>';
		}else{
			$nextPage = '<a href="'.$this->getUrl($nextNum).'">'.$this->config['next'].'</a>';
			$lastPage = '<a href="'.$this->getUrl($lastNum).'">'.$this->config['last'].'</a>';
		}

		$pageStr	 =	 str_replace(
		array('%nowPage%','%totalRow%','%totalPage%','%firstPage%','%prePage%','%linkPage%','%nextPage%','%lastPage%'),
		array($this->nowPage,$this->totalRows,$this->totalPages,$firstPage,$prePage,$linkPage,$nextPage,$lastPage),$this->config['theme']);

		$result['first'] = array('page'=>$firstNum,'url'=>$this->getUrl($firstNum));
		$result['pre'] = array('page'=>$preNum,'url'=>$this->getUrl($preNum));
		$result['link'] = $linkPage;
		$result['next'] = array('page'=>$nextNum,'url'=>$this->getUrl($nextNum));
		$result['last'] = array('page'=>$lastNum,'url'=>$this->getUrl($lastNum));
		$result['nowpage'] = $this->nowPage;
		$result['totalpage'] = (int)$this->totalPages;
		$result['total'] = $this->totalRows;
		$result['limit1'] = $this->firstRow;
		$result['limit2'] = $this->listRows;

		return $type=='string' ? $pageStr : $result;
	}

	//对页数进行循环显示，如 1 2 3 4 5
	public function pageNum(){

		//循环显示处理
		$page = $this->nowPage;//当前页
		$rollPage = $this->rollPage;//每次分页数
		$totalPages = $this->totalPages;//总页数
		$start = $page-$rollPage;
		$end = $page+$rollPage-1;
		//处理分页开始数
		if ($start <= 0) {
			$end = $end + abs($start) + 1;
			$start = 1;
		}
		//处理分页结束数
		if ($end > $totalPages) {
			$num = $end - $totalPages;//差数
			$end = $end - $num;
			$start = $start - $num;
			if ($start <= 0) {
				$start = 1;
			}
		}
		$urlStr = "";
		for ($i=$start; $i<=$end; $i++){
			if ($i == $page) {
				$urlStr.='<span class="current">'.$i.'</span> ';
			}else{
				$urlStr.='<a href="'.$this->getUrl($i).'">'.$i.'</a> ';
			}
		}
		return $urlStr;
	}

	//得到url地址
	protected function getUrl($page){
		if($this->config['url']){
			if ($this->config['index_url'] && $page==1) {
				$url = $this->config['index_url'];
			}else{
				$url = str_replace('{p}',$page,$this->config['url']);
			}
		}else{
			$depr = C('sys_url_depr');
			$htm = C('sys_url_suffix');
			if(C('sys_url_mode')){
				$url = $this->parameter.$depr.C('var_page').$depr.$page.$htm;
			}else{
				$url = $this->parameter.'&'.C('var_page').'='.$page;
			}
		}
		return $url;
	}

	//设置当前网址
	protected function setParameter($parameter){		
		$depr = C('sys_url_depr');
		
		// 如果开启了二级域名部署 则去掉url路径里面的分组参数
//		if(C('APP_MULTILEVELDOMAIN_DEPLOY_ON') && C('APP_MULTILEVELDOMAIN_DEPLOY')){
//			if ( defined('MODULE_NAME') ) {
//				$domain = trim(strtolower( MODULE_NAME ),$depr);
//				$deploys = (array)C('APP_MULTILEVELDOMAIN_DEPLOY');
//				$deploys = isset($deploys[$domain]) ? $deploys[$domain] : false;
//				if ($deploys) {
//					$_action = _APP_.'/'.CONTROLLER_NAME.$depr.ACTION_NAME;
//				}
//			}
//		}
		if (empty($_action)) $_action = _A_;
		
		//合并自定参数
		if (is_array($parameter)) {
			$new_get = array_merge($_GET,$parameter);
		}
		
		//去掉分页参数
		unset($new_get[C('var_page')]);
		
		//组装分页URL地址
		if(C('sys_url_mode')){
			foreach ($new_get as $key => $val){
				$url .= $depr.$key.$depr.$val;
			}			
		}else{			
			$url = '&'.http_build_query($new_get);
		}
		$url = $_action.$url;
		return $url;
	}
}
?>